package org.jeecg.modules.exPingbiao.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.exPingbiao.entity.ExPingbiaoExpert;
import org.jeecg.modules.exPingbiao.entity.ExPingbiaoExpertDetail;
import org.jeecg.modules.exPingbiao.entity.ExPingbiaoExpertStage;
import org.jeecg.modules.exPingbiao.mapper.ExPingbiaoExpertMapper;
import org.jeecg.modules.exPingbiao.mapper.ExPingbiaoExpertStageMapper;
import org.jeecg.modules.exPingbiao.service.IExPingbiaoExpertDetailService;
import org.jeecg.modules.exPingbiao.service.IExPingbiaoExpertStageService;
import org.jeecg.modules.exTender.entity.ExTenderList;
import org.jeecg.modules.exTender.mapper.ExTenderListMapper;
import org.jeecg.modules.exTender.service.IExTenderListService;
import org.jeecg.modules.exZbwjBidEvaluationConfig.domain.entity.ExZbwjBidEvaluationConfig;
import org.jeecg.modules.exZbwjBidEvaluationConfig.domain.entity.ExZbwjBidEvaluationItem;
import org.jeecg.modules.exZbwjBidEvaluationConfig.mapper.ExZbwjBidEvaluationConfigMapper;
import org.jeecg.modules.exZbwjBidEvaluationConfig.mapper.ExZbwjBidEvaluationItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: 评标阶段
 * @Author: jeecg-boot
 * @Date: 2024-11-09
 * @Version: V1.0
 */
@Service
public class ExPingbiaoExpertStageServiceImpl extends ServiceImpl<ExPingbiaoExpertStageMapper, ExPingbiaoExpertStage> implements IExPingbiaoExpertStageService {

    @Autowired
    private ExPingbiaoExpertStageMapper exPingbiaoExpertStageMapper;

    @Autowired
    private ExZbwjBidEvaluationItemMapper bidEvaluationItemMapper;
    @Autowired
    private ExZbwjBidEvaluationConfigMapper exZbwjBidEvaluationConfigMapper;
    @Autowired
    private ExPingbiaoExpertMapper exPingbiaoExpertMapper;
    @Autowired
    private ExTenderListMapper  exTenderListMapper;
    @Override
    public void saveRecord(String bidSectionId, String businessType, String businessId) {
        ExPingbiaoExpertStage exPingbiaoExpertStage = exPingbiaoExpertStageMapper.selectBySectionId(bidSectionId);
        if (null != exPingbiaoExpertStage) {
            return;
        }
        ExZbwjBidEvaluationConfig exZbwjBidEvaluationConfig = exZbwjBidEvaluationConfigMapper.selectByBidSectionId(bidSectionId);
        if (null == exZbwjBidEvaluationConfig) {
            return;
        }
        List<ExZbwjBidEvaluationItem> itemList = bidEvaluationItemMapper.selectByMainIdList(Lists.newArrayList(exZbwjBidEvaluationConfig.getId()));
        for (int i = 0; i < itemList.size(); i++) {
            ExZbwjBidEvaluationItem exZbwjBidEvaluationItem = itemList.get(i);
            if (Objects.equals(exZbwjBidEvaluationItem.getId(), businessId)) {
                exPingbiaoExpertStage = new ExPingbiaoExpertStage();
                exPingbiaoExpertStage.setBidSectionId(bidSectionId);
                exPingbiaoExpertStage.setBusinessId(exZbwjBidEvaluationItem.getId());
                exPingbiaoExpertStage.setBusinessType(exZbwjBidEvaluationItem.getType());
                exPingbiaoExpertStageMapper.insert(exPingbiaoExpertStage);
            }
        }
    }


    @Override
    public Result<String> updateRecordToNext(String bidSectionId) {
        ExPingbiaoExpertStage exPingbiaoExpertStage = exPingbiaoExpertStageMapper.selectBySectionId(bidSectionId);
        if (null == exPingbiaoExpertStage) {
            return Result.error("数据不存在");
        }
        if (exPingbiaoExpertStage.getIsSummary() == 0) {
            exPingbiaoExpertStage.setIsSummary(1);
            exPingbiaoExpertStageMapper.updateById(exPingbiaoExpertStage);
            //如果存在不符合项结论项 ，将对应投标人记录 标记为不符合
            if ("2".equals(exPingbiaoExpertStage.getBusinessType())) {
                List<String> tenderIdList = exPingbiaoExpertMapper.selectRejectTenderIdList(bidSectionId, exPingbiaoExpertStage.getBusinessId());
                if(CollectionUtil.isNotEmpty(tenderIdList)){
                    exTenderListMapper.updateRejectFlag(tenderIdList);
                }
            }
            return Result.ok();
        }
        //开评标配置
        ExZbwjBidEvaluationConfig exZbwjBidEvaluationConfig = exZbwjBidEvaluationConfigMapper.selectByBidSectionId(bidSectionId);
        if (null == exZbwjBidEvaluationConfig) {
            return Result.error("数据不存在");
        }
        List<ExZbwjBidEvaluationItem> itemList = bidEvaluationItemMapper.selectByMainIdList(Lists.newArrayList(exZbwjBidEvaluationConfig.getId()));
        Integer nextIndex = null;
        for (int i = 0; i < itemList.size(); i++) {
            ExZbwjBidEvaluationItem exZbwjBidEvaluationItem = itemList.get(i);
            if (Objects.equals(exZbwjBidEvaluationItem.getId(), exPingbiaoExpertStage.getBusinessId())) {
                if (i != itemList.size() - 1) {
                    nextIndex = i + 1;
                }
            }
        }
        if (null != nextIndex) {
            ExZbwjBidEvaluationItem item = itemList.get(nextIndex);
            ExPingbiaoExpertStage updateStage = new ExPingbiaoExpertStage();
            updateStage.setId(exPingbiaoExpertStage.getId());
            updateStage.setBusinessId(item.getId());
            updateStage.setBusinessType(item.getType());
            updateStage.setIsSummary(0);
            exPingbiaoExpertStageMapper.updateById(updateStage);
        }
        return Result.ok();
    }

    @Override
    public ExPingbiaoExpertStage selectBySectionId(String sectionId) {
        ExPingbiaoExpertStage exPingbiaoExpertStage = exPingbiaoExpertStageMapper.selectBySectionId(sectionId);
        return exPingbiaoExpertStage;
    }
}
